package com.sea.app.common.kit;

import com.sea.app.common.excrption.SystemException;
import com.sea.app.common.kit.signature.MD5;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
import java.util.function.Function;

/**
 * Created by 31417 on 2017/4/23.
 */
public class Strings implements java.io.Serializable, Comparable<String>, CharSequence{
    public static final String CHARSET = "UTF-8";
    private String value;

    private Strings(String value){
        this.value = value;
    }

    public static Strings onRandom(){
        return Strings.on(UUID.randomUUID().toString());
    }


    public static Strings on(String value){
        return new Strings(value);

    }
    public static Strings on(Strings value){
        return new Strings(value.toString());
    }
    public static Strings on(byte[] s){
        try {
            return Strings.on(new String(s,CHARSET));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw SystemException.on(e);
        }
    }

    /**
     * byte[] 转换为字符串
     * @param data
     * @return
     */
    public static Strings onHex(byte[] data){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(data[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return Strings.on(sb.toString());
    }
    /**
     * base64字符串转换为普通字符串
     * @param s
     * @return
     */
    public static Strings onBase64(String s){
        try {
            return Strings.on(new sun.misc.BASE64Decoder().decodeBuffer(s));
        } catch (IOException e) {
            e.printStackTrace();
            throw SystemException.on(e);
        }
    }



    public boolean isNull(){
        return value == null;
    }
    public boolean isBlank(){
        return isNull()||value.trim().isEmpty();
    }

    public Strings isBlank(Function<Strings,Strings> function){
        if(isBlank()){
            return Strings.on(function.apply(this));
        }
        return this;
    }

    public Strings notBlank(Function<Strings,Strings> function){
        if(!isBlank()){
            return Strings.on(function.apply(this));
        }
        return this;
    }

    public Strings endWith(String suffix,Function<Strings,Strings> function){
        if(endWith(suffix)){
           function.apply(this);
        }
        return this;
    }

    public Strings mustSuffix(String suffix){
        if(!endWith(suffix)){
            joinEnd(suffix);
        }
        return this;
    }

    public Strings mustPrefix(String prefix){
        if(!startWith(prefix)){
            joinStart(prefix);
        }
        return this;
    }

    public Strings joinEnd(String string){
        return Strings.on(this.value + string);
    }
    public Strings joinStart(String string){
        return Strings.on(string+this.value);
    }

    /**
     * 如果为空就给空字符串
     * @return
     */
    public Strings defaultString(){
        return defaultString("");
    }

    /**
     * 如果为NULl就给默认值
     * @param def
     * @return
     */
    public Strings defaultString(String def){
        return Strings.on(this.value==null?def:this.value);
    }

    /**
     * 删除开始指定长度的字符
     * @param length
     * @return
     */
    public Strings removeStart(int length){
        if(value.length()<length){
            return this;
        }
        return Strings.on(value.substring(length, value.length()));
    }

    /**
     * 删除结尾指定的字符
     * @param length
     * @return
     */
    public Strings removeEnd(int length){
        if(value.length()<length){
            return this;
        }
        return Strings.on(value.substring(0, value.length()-length));
    }
    public Strings replace(CharSequence target, CharSequence replacement){
        return Strings.on(value.replace(target,replacement));
    }
    public Strings md5(){
        return Strings.onHex(MD5.compute(toByte()));
    }

    public byte[] toByte(){
        try {
            return value.getBytes(CHARSET);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw SystemException.on(e);
        }
    }

    public boolean endWith(String suffix){
        return value.endsWith(suffix);
    }
    public boolean startWith(String prefix){
        return value.startsWith(prefix);
    }

    public Strings base64(){
        return Strings.on(new sun.misc.BASE64Encoder().encode(toByte()));
    }
    public byte[] toBase64Byte(){
        return base64().toByte();
    }


    @Override
    public int hashCode() {
        return value.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj==null){
            return value == null;
        }
        if(obj instanceof String)
            return value.equals(obj);
        if(obj instanceof Strings){
            return value.equals(obj.toString());
        }
        return super.equals(obj);
    }

    @Override
    public int length() {
        return value.length();
    }

    @Override
    public char charAt(int index) {
        return value.charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return value.subSequence(start,end);
    }

    @Override
    public String toString() {
        return value;
    }

    @Override
    public int compareTo(String o) {
        return value.compareTo(o);
    }
}
